package org.wkg.lesson07;

public class MyBlockingQueue02 {
    private Integer[] element;
    private volatile int head;
    private volatile int tail;
    private volatile int size;

    public MyBlockingQueue02(int value) {
        if (value <= 0) {
            throw new IllegalArgumentException("不能输入小于等于0的数");
        }
        element = new Integer[value];
    }

    public synchronized void put(int value) throws InterruptedException {
        while (size >= element.length) {
            this.wait();
        }
        element[tail] = value;
        tail++;
        if (tail >= element.length) {
            tail = 0;
        }
        size++;
        notifyAll();
    }

    public synchronized int take() throws InterruptedException {
        while (size <= 0) {
            this.wait();
        }
        Integer value = element[head];
        head++;
        if (head >= element.length) {
            head = 0;
        }
        size--;
        notifyAll();
        return value;
    }


}
